<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"dttx123.gitee.io","root":"/blog/","scheme":"Gemini","version":"8.0.0-rc.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="基本概念为什么要有数据库没有数据库，我们的数据都是存储在文件当中的，那么文件存储数据的缺点有：  文件的安全性问题。 文件不利于查询和对数据的管理。 文件不利于存放海量数据 文件在程序中控制不方便  什么是数据库数据库，简而言之可视为电子化的文件柜——存储电子文件的处所，用户可以对文件中的数据运行增加、删除、修改、查询等操作。 前端程序员只需要对数据库有一定了解即可。 浏览器—-&gt;服务器—-">
<meta property="og:type" content="article">
<meta property="og:title" content="sql 基础">
<meta property="og:url" content="http://dttx123.gitee.io/2018/08/01/nodejs/04-mysq/index.html">
<meta property="og:site_name" content="Starry Night">
<meta property="og:description" content="基本概念为什么要有数据库没有数据库，我们的数据都是存储在文件当中的，那么文件存储数据的缺点有：  文件的安全性问题。 文件不利于查询和对数据的管理。 文件不利于存放海量数据 文件在程序中控制不方便  什么是数据库数据库，简而言之可视为电子化的文件柜——存储电子文件的处所，用户可以对文件中的数据运行增加、删除、修改、查询等操作。 前端程序员只需要对数据库有一定了解即可。 浏览器—-&gt;服务器—-">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2018-07-31T16:00:00.000Z">
<meta property="article:modified_time" content="2020-05-13T03:35:51.208Z">
<meta property="article:author" content="大春哥">
<meta property="article:tag" content="node  数据库 sql">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://dttx123.gitee.io/2018/08/01/nodejs/04-mysq/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>sql 基础 | Starry Night</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line toggle-line-first"></span>
        <span class="toggle-line toggle-line-middle"></span>
        <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Starry Night</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">这就朕为你打下的江山</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://dttx123.gitee.io/2018/08/01/nodejs/04-mysq/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
      <meta itemprop="name" content="大春哥">
      <meta itemprop="description" content="IT">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Starry Night">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          sql 基础
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2018-08-01 00:00:00" itemprop="dateCreated datePublished" datetime="2018-08-01T00:00:00+08:00">2018-08-01</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-13 11:35:51" itemprop="dateModified" datetime="2020-05-13T11:35:51+08:00">2020-05-13</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" itemprop="url" rel="index"><span itemprop="name">数据库</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h1><h2 id="为什么要有数据库"><a href="#为什么要有数据库" class="headerlink" title="为什么要有数据库"></a>为什么要有数据库</h2><p>没有数据库，我们的数据都是存储在文件当中的，那么文件存储数据的缺点有：</p>
<ul>
<li>文件的安全性问题。</li>
<li>文件不利于查询和对数据的管理。</li>
<li>文件不利于存放海量数据</li>
<li>文件在程序中控制不方便</li>
</ul>
<h2 id="什么是数据库"><a href="#什么是数据库" class="headerlink" title="什么是数据库"></a>什么是数据库</h2><p>数据库，简而言之可视为电子化的文件柜——存储电子文件的处所，用户可以对文件中的数据运行增加、删除、修改、查询等操作。</p>
<p>前端程序员只需要对数据库有一定了解即可。</p>
<p><strong>浏览器—-&gt;服务器—-&gt;数据库</strong></p>
<h2 id="数据库的分类"><a href="#数据库的分类" class="headerlink" title="数据库的分类"></a>数据库的分类</h2><p>关系型数据库：</p>
<ul>
<li><strong>MySQL</strong>、</li>
<li>Oracle、</li>
<li>SQL Server</li>
<li>SQLite（安卓）</li>
</ul>
<p>非关系型数据库</p>
<ul>
<li>mongodb</li>
<li>redis</li>
<li>BigTable</li>
</ul>
<p>DBA</p>
<h2 id="数据库中基本术语"><a href="#数据库中基本术语" class="headerlink" title="数据库中基本术语"></a>数据库中基本术语</h2><ul>
<li>数据库<code>database</code>：存放数据的仓库，一般一个项目中的数据会存储到一个数据库中</li>
<li>表<code>table</code>： 一个表对应一类数据，比如学生表，老师表</li>
<li>列<code>columns</code>:一张表由多列组成，也叫一个字段，比如学生的姓名，成绩，年龄等</li>
<li>行<code>rows</code>: 一个学生信息对应一行，一行也叫一条记录。</li>
</ul>
<h1 id="数据库的可视化操作"><a href="#数据库的可视化操作" class="headerlink" title="数据库的可视化操作"></a>数据库的可视化操作</h1><h2 id="创建数据库"><a href="#创建数据库" class="headerlink" title="创建数据库"></a>创建数据库</h2><h2 id="创建表"><a href="#创建表" class="headerlink" title="创建表"></a>创建表</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">存储以下学生信息</span><br><span class="line"></span><br><span class="line">&#123;id: 1, name: &#39;张三&#39;, age: 18, gender: &#39;男&#39;, content: &#39;这是描述信息‘&#125;</span><br></pre></td></tr></table></figure>



<h2 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h2><p><code>int</code>: 整数类型</p>
<p><code>varchar</code>: 字符类型</p>
<p><code>datetime</code>: 日期类型</p>
<h1 id="数据库的常见命令"><a href="#数据库的常见命令" class="headerlink" title="数据库的常见命令"></a>数据库的常见命令</h1><blockquote>
<p>SQL: 结构化查询语言(Structured Query Language)简称SQL  。用于数据库的增删改查以及管理等功能。</p>
</blockquote>
<h2 id="数据库相关"><a href="#数据库相关" class="headerlink" title="数据库相关"></a>数据库相关</h2><ul>
<li><p><code>--</code>SQL中的注释</p>
</li>
<li><p><code>SHOW DATABASES;</code> 查看所有的数据</p>
</li>
<li><p><code>CREATE DATABASE mydb;</code> 创建数据库</p>
</li>
<li><p><code>DROP DATABASE mydb;</code>删除数据库</p>
</li>
<li><p><code>USE mydb;</code> 使用数据库</p>
</li>
</ul>
<h2 id="表相关"><a href="#表相关" class="headerlink" title="表相关"></a>表相关</h2><ul>
<li><code>SHOW TABLES;</code>查看当前数据库中所有的表</li>
<li>创建表</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="keyword">user</span>(</span><br><span class="line">	<span class="keyword">id</span> <span class="built_in">INT</span> auto_increment PRIMARY <span class="keyword">KEY</span>,</span><br><span class="line">	<span class="keyword">name</span> <span class="built_in">VARCHAR</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line">	age <span class="built_in">INT</span> ,</span><br><span class="line">	gender <span class="built_in">VARCHAR</span>(<span class="number">4</span>),</span><br><span class="line">	<span class="keyword">content</span> <span class="built_in">VARCHAR</span>(<span class="number">255</span>)</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<ul>
<li><code>DROP TABLE user;</code>删除表</li>
</ul>
<h2 id="插入数据"><a href="#插入数据" class="headerlink" title="插入数据"></a>插入数据</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> <span class="keyword">user</span> (<span class="keyword">name</span>, age, gender, <span class="keyword">content</span>) <span class="keyword">VALUES</span> (<span class="string">'胡聪聪'</span>, <span class="number">18</span>, <span class="string">'男'</span>, <span class="string">'哈哈哈，哈哈哈'</span>)</span><br><span class="line"><span class="comment">-- 如果省略列名，那么必须要和字段一一对应</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> <span class="keyword">user</span> <span class="keyword">VALUES</span> (<span class="literal">null</span>, <span class="string">'胡聪聪'</span>, <span class="number">18</span>, <span class="string">'男'</span>, <span class="string">'哈哈哈，哈哈哈'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> <span class="keyword">user</span> <span class="keyword">SET</span> <span class="keyword">name</span>=<span class="string">'hcc'</span>, age=<span class="number">18</span>, gender=<span class="string">'男'</span>, <span class="keyword">content</span>=<span class="string">'嘻嘻嘻'</span></span><br></pre></td></tr></table></figure>

<h2 id="修改数据"><a href="#修改数据" class="headerlink" title="修改数据"></a>修改数据</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">// 修改所有的数据</span><br><span class="line"><span class="keyword">UPDATE</span> <span class="keyword">USER</span> <span class="keyword">SET</span> <span class="keyword">name</span>=<span class="string">'胡西西'</span></span><br><span class="line">// 根据条件修改</span><br><span class="line"><span class="keyword">UPDATE</span> <span class="keyword">USER</span> <span class="keyword">set</span> <span class="keyword">name</span>=<span class="string">'胡聪聪'</span>, <span class="keyword">content</span>=<span class="string">"这是内容"</span> <span class="keyword">WHERE</span> <span class="keyword">id</span> = <span class="number">2</span></span><br></pre></td></tr></table></figure>



<h2 id="删除数据"><a href="#删除数据" class="headerlink" title="删除数据"></a>删除数据</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// 删除所有的数据</span><br><span class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> <span class="keyword">USER</span></span><br><span class="line"></span><br><span class="line">// 删除<span class="keyword">id</span>为<span class="number">5</span>的数据</span><br><span class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> <span class="keyword">USER</span> <span class="keyword">WHERE</span> <span class="keyword">id</span> = <span class="number">5</span></span><br></pre></td></tr></table></figure>



<h2 id="查询数据"><a href="#查询数据" class="headerlink" title="查询数据"></a>查询数据</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询所有数据</span></span><br><span class="line"><span class="keyword">SELECT</span> * <span class="keyword">FROM</span> <span class="keyword">user</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询指定列</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">id</span>, <span class="keyword">name</span>,age <span class="keyword">from</span> <span class="keyword">user</span></span><br></pre></td></tr></table></figure>



<h2 id="条件查询"><a href="#条件查询" class="headerlink" title="条件查询"></a>条件查询</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--- 并且</span></span><br><span class="line"><span class="keyword">SELECT</span> * <span class="keyword">from</span> <span class="keyword">user</span> <span class="keyword">where</span> <span class="keyword">name</span>=<span class="string">'胡聪聪'</span> <span class="keyword">AND</span> age=<span class="number">21</span></span><br><span class="line"></span><br><span class="line"><span class="comment">--- 或者</span></span><br><span class="line"><span class="keyword">SELECT</span> * <span class="keyword">from</span> <span class="keyword">user</span> <span class="keyword">where</span> <span class="keyword">name</span>=<span class="string">'胡聪聪'</span> <span class="keyword">or</span> age=<span class="number">21</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 范围查询</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 模糊查询  %表示通配  _表示单个字符</span></span><br><span class="line"><span class="keyword">SELECT</span> * <span class="keyword">from</span> <span class="keyword">user</span> <span class="keyword">where</span> <span class="keyword">name</span> <span class="keyword">LIKE</span> <span class="string">'胡%'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- in语句</span></span><br><span class="line"><span class="keyword">SELECT</span> * <span class="keyword">from</span> <span class="keyword">user</span> <span class="keyword">where</span> <span class="keyword">name</span> <span class="keyword">in</span> (<span class="string">'胡聪聪'</span>, <span class="string">'hcc'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">-- order by</span></span><br><span class="line"><span class="comment">-- 排序需要写在最后面，，asc升序  desc：降序</span></span><br><span class="line"><span class="keyword">SELECT</span> * <span class="keyword">from</span> <span class="keyword">user</span> <span class="keyword">ORDER</span> <span class="keyword">BY</span> <span class="keyword">id</span> <span class="keyword">desc</span></span><br></pre></td></tr></table></figure>



<h2 id="导入和导出数据库脚本"><a href="#导入和导出数据库脚本" class="headerlink" title="导入和导出数据库脚本"></a>导入和导出数据库脚本</h2><h1 id="node操作mysql"><a href="#node操作mysql" class="headerlink" title="node操作mysql"></a>node操作mysql</h1><h2 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h2><ul>
<li>安装</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install mysql</span><br></pre></td></tr></table></figure>

<ul>
<li>基本使用</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 导入第三方包</span></span><br><span class="line"><span class="keyword">const</span> mysql = <span class="built_in">require</span>(<span class="string">'mysql'</span>)</span><br><span class="line"><span class="comment">// 创建连接</span></span><br><span class="line"><span class="keyword">var</span> connection = mysql.createConnection(&#123;</span><br><span class="line">  <span class="comment">// 本地</span></span><br><span class="line">  host: <span class="string">'localhost'</span>,</span><br><span class="line">  user: <span class="string">'root'</span>,</span><br><span class="line">  password: <span class="string">'root'</span>,</span><br><span class="line">  <span class="comment">// 数据库名称</span></span><br><span class="line">  database: <span class="string">'mydb'</span>,</span><br><span class="line">  port: <span class="number">3306</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 连接数据库</span></span><br><span class="line">connection.connect()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 执行sql语句</span></span><br><span class="line">connection.query(<span class="string">'select * from user where id = 8'</span>, (err, result) =&gt; &#123;</span><br><span class="line">  <span class="keyword">if</span> (err) <span class="keyword">return</span> <span class="built_in">console</span>.log(<span class="string">'查询失败'</span>, err)</span><br><span class="line">  <span class="comment">// result返回的是数组， 数组中是一个对象</span></span><br><span class="line">  <span class="built_in">console</span>.log(result)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 关闭连接</span></span><br><span class="line">connection.end()</span><br></pre></td></tr></table></figure>



<h2 id="查询语句"><a href="#查询语句" class="headerlink" title="查询语句"></a>查询语句</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> name = <span class="string">'胡聪聪'</span></span><br><span class="line"><span class="comment">// 使用?表示占位，</span></span><br><span class="line">connect.query(<span class="string">`select * from user where name=?`</span>, name, (err, result) =&gt; &#123;</span><br><span class="line">  <span class="keyword">if</span> (err) <span class="keyword">return</span> <span class="built_in">console</span>.log(<span class="string">'错误了'</span>, err)</span><br><span class="line">  <span class="built_in">console</span>.log(result)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h2 id="插入语句"><a href="#插入语句" class="headerlink" title="插入语句"></a>插入语句</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">connect.query(</span><br><span class="line">  <span class="string">'insert into user (name, age, gender, content) values (?, ?, ?, ?)'</span>,</span><br><span class="line">  [<span class="string">'胡嘻嘻'</span>, <span class="number">18</span>, <span class="string">'男'</span>, <span class="string">'哈哈哈哈'</span>],</span><br><span class="line">  err =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">return</span> <span class="built_in">console</span>.log(<span class="string">'错误'</span>, err)</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'添加成功了'</span>)</span><br><span class="line">  &#125;</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 方式2</span></span><br><span class="line">connect.query(</span><br><span class="line">  <span class="string">'insert into user values ?'</span>,</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">'胡洗洗'</span>,</span><br><span class="line">    age: <span class="number">30</span>,</span><br><span class="line">    gender: <span class="string">'男'</span>,</span><br><span class="line">    content: <span class="string">'哈哈哈'</span></span><br><span class="line">  &#125;,</span><br><span class="line">  (err, result) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">return</span> <span class="built_in">console</span>.log(<span class="string">'错误'</span>, err)</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'添加成功了'</span>, result)</span><br><span class="line">  &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>



<h2 id="修改语句"><a href="#修改语句" class="headerlink" title="修改语句"></a>修改语句</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">connect.query(</span><br><span class="line">  <span class="string">'update user set ? where id = ?'</span>,</span><br><span class="line">  [</span><br><span class="line">    &#123;</span><br><span class="line">      name: <span class="string">'胡洗洗'</span>,</span><br><span class="line">      age: <span class="number">30</span>,</span><br><span class="line">      gender: <span class="string">'男'</span>,</span><br><span class="line">      content: <span class="string">'哈哈哈'</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="number">10</span></span><br><span class="line">  ],</span><br><span class="line">  (err, result) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">return</span> <span class="built_in">console</span>.log(<span class="string">'错误'</span>, err)</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'添加成功了'</span>, result)</span><br><span class="line">  &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>



<h2 id="删除语句"><a href="#删除语句" class="headerlink" title="删除语句"></a>删除语句</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">connect.query(<span class="string">'delete from user where id = ?'</span>, [<span class="number">10</span>], (err, result) =&gt; &#123;</span><br><span class="line">  <span class="keyword">if</span> (err) <span class="keyword">return</span> <span class="built_in">console</span>.log(<span class="string">'失败'</span>, err)</span><br><span class="line">  <span class="built_in">console</span>.log(result)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>





<h2 id="db模块封装"><a href="#db模块封装" class="headerlink" title="db模块封装"></a>db模块封装</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> mysql = <span class="built_in">require</span>(<span class="string">'mysql'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 配置相关数据</span></span><br><span class="line"><span class="keyword">var</span> config = &#123;</span><br><span class="line">  host: <span class="string">'127.0.0.1'</span>,</span><br><span class="line">  user: <span class="string">'root'</span>,</span><br><span class="line">  password: <span class="string">'root'</span>,</span><br><span class="line">  database: <span class="string">'ztest'</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports.myQuery = <span class="function"><span class="keyword">function</span> (<span class="params">sql, param, fn</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 1- 创建连接</span></span><br><span class="line">  <span class="keyword">const</span> con = mysql.createConnection(config);</span><br><span class="line">  <span class="comment">// 2- 连接</span></span><br><span class="line">  con.connect();</span><br><span class="line">  <span class="comment">// 3- 操作 </span></span><br><span class="line">  con.query(sql, param, (err, data) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">console</span>.log(err);</span><br><span class="line">    &#125;</span><br><span class="line">    fn &amp;&amp; fn(data);</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="comment">// 4- 断开</span></span><br><span class="line">  con.end();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/node-%E6%95%B0%E6%8D%AE%E5%BA%93-sql/" rel="tag"># node  数据库 sql</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2018/07/07/nodejs/03-express%E7%AC%94%E8%AE%B0/" rel="prev" title="express">
      <i class="fa fa-chevron-left"></i> express
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2018/08/22/vue/vue-router/" rel="next" title="vue 基础">
      vue 基础 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#基本概念"><span class="nav-text">基本概念</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#为什么要有数据库"><span class="nav-text">为什么要有数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是数据库"><span class="nav-text">什么是数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库的分类"><span class="nav-text">数据库的分类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库中基本术语"><span class="nav-text">数据库中基本术语</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#数据库的可视化操作"><span class="nav-text">数据库的可视化操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#创建数据库"><span class="nav-text">创建数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建表"><span class="nav-text">创建表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据类型"><span class="nav-text">数据类型</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#数据库的常见命令"><span class="nav-text">数据库的常见命令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库相关"><span class="nav-text">数据库相关</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#表相关"><span class="nav-text">表相关</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#插入数据"><span class="nav-text">插入数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#修改数据"><span class="nav-text">修改数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除数据"><span class="nav-text">删除数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#查询数据"><span class="nav-text">查询数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#条件查询"><span class="nav-text">条件查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#导入和导出数据库脚本"><span class="nav-text">导入和导出数据库脚本</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#node操作mysql"><span class="nav-text">node操作mysql</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#基本使用"><span class="nav-text">基本使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#查询语句"><span class="nav-text">查询语句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#插入语句"><span class="nav-text">插入语句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#修改语句"><span class="nav-text">修改语句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除语句"><span class="nav-text">删除语句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#db模块封装"><span class="nav-text">db模块封装</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="大春哥"
      src="/blog/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">大春哥</p>
  <div class="site-description" itemprop="description">IT</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">35</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">大春哥</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

</body>
</html>
